import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/*

  数据展示：

    调用业务层
 */
public class View {


   private  Business business;

    public View(Business business) {
        this.business = business;
    }

    //展示数据
    public void showCustomers(){
        Map<String, Customer> customerMap = business.queryCustomers();
        System.out.println("客户编号\t客户名称\t\t\t\t客户生日\t\t\t\t客户性别\t\t注册时间");
        Set<String> keys = customerMap.keySet();
        DateFormat df = new SimpleDateFormat("yyyy年MM月dd日");
        for(String key:keys){
            Customer customer = customerMap.get(key);
            System.out.println();
            System.out.printf("%-8s%-19s%-17s%2c%19s",key,customer.getName(),df.format(customer.getBirthday()),customer.getGender(),df.format(customer.getRegistTime()));
            System.out.println();
        }
    }
    //增加数据
    public void addCustomer() throws ParseException, IOException {
        System.out.println("请输入客户编号:(例1002)");
        String id=new Scanner(System.in).nextLine();
        System.out.println("请输入客户名称:");
        String name=new Scanner(System.in).nextLine();
        System.out.println("请输入客户生日:(yyyy年MM月dd日)");
        String day=new Scanner(System.in).nextLine();
        DateFormat df = new SimpleDateFormat("yyyy年MM月dd日");
        Date birthday=df.parse(day);
        System.out.println("请输入客户性别:(男/女)");
        char gender=new Scanner(System.in).next().charAt(0);
        Calendar calendar = Calendar.getInstance();
        Date calendarTime = calendar.getTime();
        business.addCustomer(id,name,birthday,gender,calendarTime);
    }
    //删除数据
    public void deleteCustomer() throws IOException {
        System.out.println("请输入你要删除的客户的客户编号:");
        String id=new Scanner(System.in).next();
        business.deleteCustomer(id);
    }
    //批量删除数据
    public void batchDeleteCustomer() throws IOException {
        System.out.println("请输入你要批量删除的客户的客户编号，以/连接(例1001/1002/1003):");
        String ids=new Scanner(System.in).next();
        String[] strings = ids.split("/");
        business.batchDelete(strings);
    }
    //修改数据
    public void modifyCustomer() throws ParseException, IOException {
        System.out.println("请输入需要修改的客户编号:(例1002)");
        String id=new Scanner(System.in).nextLine();
        System.out.println("请输入新的客户名称:");
        String name=new Scanner(System.in).nextLine();
        System.out.println("请输入新的客户生日:(yyyy年MM月dd日)");
        String day=new Scanner(System.in).nextLine();
        DateFormat df = new SimpleDateFormat("yyyy年MM月dd日");
        Date birthday=df.parse(day);
        System.out.println("请输入新的客户性别:(男/女)");
        char gender=new Scanner(System.in).next().charAt(0);
        business.modifyCustomer(id,name,birthday,gender);
    }
    //查找数据
    public void checkCustomer(){
        System.out.println("请输入需要查找的客户编号:(例1002)");
        String id=new Scanner(System.in).nextLine();
        Customer customer=business.checkCustomer(id);
        if (customer!=null){
            System.out.println(customer);
        }else {
            System.out.println("查无此人!");
        }
    }

    public static void main(String[] args) throws IOException, ParseException {

        //初始化数据
       /* DateFormat df = new SimpleDateFormat("yyyy年MM月dd日");
        Map<String,Customer> map = new HashMap<>();

//                                  String id, String name, Date birthday, char gender
        map.put("1001",new Customer("1001","张三",df.parse("1999年3月1日"),'男'));
        map.put("1002",new Customer("1002","李四",df.parse("2001年10月1日"),'男'));
        //序列化
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream("src\\data.txt"));
        objectOutputStream.writeObject(map);*/


        View view = new View(new Business(new DataDao()));
        System.out.println("======================客户管理系统========================");
        System.out.println("========================显示=============================");
        view.showCustomers();

        while (true) {
            System.out.println("=======================系统操作==========================");
            System.out.println("=====================输入1：增加客户======================");
            System.out.println("=====================输入2：删除客户======================");
            System.out.println("=====================输入3：批量删除======================");
            System.out.println("=====================输入4：修改客户======================");
            System.out.println("=====================输入5：查看客户======================");
            System.out.println("=====================输入6：显示信息======================");
            System.out.println("=====================输入0：退出系统======================");
            System.out.print("请输入操作的代号:");
            Scanner scanner = new Scanner(System.in);
            int n=scanner.nextInt();
            switch (n) {
                case 1:
                    view.addCustomer();
                    break;
                case 2:
                    view.deleteCustomer();
                    break;
                case 3:
                    view.batchDeleteCustomer();
                    break;
                case 4:
                    view.modifyCustomer();
                    break;
                case 5:
                    view.checkCustomer();
                    break;
                case 6:
                    view.showCustomers();
                    break;
                case 0:
                    return;
            }
        }

    }

}
